import CFG from "./config.js"
var CryptoJS = require('./crypto-js.js')
function http_build_query(array, encode) {
  var list = [];
  for (var i in array) {
    // 代码完善：如果值为JSON对象的需要转换为JSON字符串 @Thu Feb 16 10:03:33 CST 2017 by Shaxt
    if ('object' === typeof array[i]) {
      array[i] = JSON.stringify(array[i]);
    }
    var value = encode ? encodeURIComponent(array[i]) : array[i];
    var str = i + '=' + value;
    list.push(str);
  }
  return list.join('&');
}
function desEncrypt(data){
  if (CFG.APP_ENCRYPT) {
    if (typeof data == 'object') {
      data = http_build_query(data);
    }
    //data = DES3.encrypt(CFG.DEX3_KEY, data, CFG.IV_KEY);  加密修改
    let key = pad(CFG.DEX3_KEY,24)
    let iv = CFG.IV_KEY
    key = CryptoJS.enc.Utf8.parse(key);
    iv = CryptoJS.enc.Utf8.parse(iv);
    var encrypted = CryptoJS.TripleDES.encrypt(data, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    })
    data =  encrypted.toString();
    data = { 'p': data };
  }
  return data;
}
function pad(num, n) {
  var len = num.toString().length;
  while(len < n) {
    num = num +'0';
    len++;
  }
  return num;
}
export {
	desEncrypt
}